﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using ViCode_LeVi.Reports;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabBalanceAndOthers_InvestmentPlan.xaml
    /// </summary>
    public partial class VMTabBalanceAndOthers_InvestmentPlan : UserControl
    {
        public VMTabBalanceAndOthers_InvestmentPlan()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(VMTabBalanceAndOthers_Balance_Loaded);
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(VMTabBalanceAndOthers_InvestmentPlan_DataContextChanged);
        }

        void VMTabBalanceAndOthers_InvestmentPlan_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            RefreshView();
        }
        Step_BalanceAndOthers_InvestmentPlan _investmentPlan = null;
        void VMTabBalanceAndOthers_Balance_Loaded(object sender, RoutedEventArgs e)
        {
            RefreshView();
        }
        private void RefreshView()
        {
            _investmentPlan = this.DataContext as Step_BalanceAndOthers_InvestmentPlan;
            if (_investmentPlan != null)
            {
                _investmentPlan.RefreshData();
                GenUI(_investmentPlan);
                SetupButton_AddNew();
            }
        }
        #region Gen
        private void GenUI(Step_BalanceAndOthers_InvestmentPlan balance)
        {
            if (balance == null)
                return;
            if (balance.Items_INVESTMENT == null || balance.Items_INVESTMENT.Length == 0)
            {
                balance.AddNew(balance.GROUP_INVESTMENT);
            }
            GenUI(Panel_Investment, balance.Items_INVESTMENT);

            if (balance.Items_SELL == null || balance.Items_SELL.Length == 0)
            {
                balance.AddNew(balance.GROUP_SELL);
            }
            GenUI(Panel_Sell, balance.Items_SELL);

          
        }
        private void GenUI(Panel panel, Step_BalanceAndOthers_InvestmentPlan_Item[] items)
        {
            if (items == null || items.Length == 0)
            {
                panel.Children.Clear();
            }
            else
            {
                ////Collapse all
                //foreach(FrameworkElement child in panel.Children)
                //{
                //    child.Visibility = Visibility.Collapsed;
                //}
                //add
                var children = panel.Children.Cast<FrameworkElement>().ToArray();
                List<FrameworkElement> listInUI = new List<FrameworkElement>();
                foreach (var item in items)
                {
                    var child = children.FirstOrDefault(d => d.DataContext == item);
                    if (child == null)
                    {
                        child = new VMTabBalanceAndOthers_InvestmentPlan_Item()
                        {
                            DataContext = item
                              ,
                            Action_Remove = (removeItem) =>
                            {
                                if (_investmentPlan != null)
                                {
                                    if (MessageBox.Show("Do you want to remove this item?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                    {
                                        _investmentPlan.Remove(removeItem);
                                        if (removeItem.Parent != null)
                                            removeItem.Parent.SendPropertyChanged_Total();
                                        GenUI(_investmentPlan);
                                    }
                                }
                            }
                        };
                        panel.Children.Add(child);
                    }
                    listInUI.Add(child);
                }
                //remove old
                List<FrameworkElement> listWillRemove = new List<FrameworkElement>();
                foreach (FrameworkElement child in panel.Children)
                {
                    if (!listInUI.Contains(child))
                        listWillRemove.Add(child);
                }
                foreach (var removeItem in listWillRemove)
                {
                    panel.Children.Remove(removeItem);
                }
            }
        }
        #endregion

        #region Button add new
        private void SetupButton_AddNew()
        {
            Button_AddNew_Investment.Click -= new RoutedEventHandler(Button_AddNew_Investment_Click);
            Button_AddNew_Sell.Click -= new RoutedEventHandler(Button_AddNew_Sell_Click);
            Button_AddNew_Investment.Click += new RoutedEventHandler(Button_AddNew_Investment_Click);
            Button_AddNew_Sell.Click += new RoutedEventHandler(Button_AddNew_Sell_Click);
        }

     
        void Button_AddNew_Sell_Click(object sender, RoutedEventArgs e)
        {
            if (_investmentPlan != null)
                Button_AddNew(_investmentPlan.GROUP_SELL);
        }

        void Button_AddNew_Investment_Click(object sender, RoutedEventArgs e)
        {
            if (_investmentPlan != null)
                Button_AddNew(_investmentPlan.GROUP_INVESTMENT);
        }
        void Button_AddNew(string grpName)
        {
            if (_investmentPlan != null)
            {
                _investmentPlan.AddNew(grpName);
                GenUI(_investmentPlan);
            }
        }
        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Report_InvestmentPlan rep = new Report_InvestmentPlan();
            var datasource = new List<Step_BalanceAndOthers_InvestmentPlan>();
            datasource.Add(_investmentPlan);
            frmViewReport frm = new frmViewReport(rep, datasource);
            frm.Show();
        }
    }
}
